/**
 * There are code generation
 */
package com.neusoft.lohas.modules.ivm.service.mall;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.lohas.common.persistence.Page;
import com.neusoft.lohas.common.service.BaseService;
import com.neusoft.lohas.modules.ivm.dao.mall.IndexPhotoDao;
import com.neusoft.lohas.modules.ivm.entity.mall.IndexPhoto;

/**
 * 商城PC端热点图片Service
 * @author Jackie.Liu
 * @version 2015-04-09
 */
@Component
@Transactional(readOnly = true)
public class IndexPhotoService extends BaseService {

	@Autowired
	private IndexPhotoDao indexPhotoDao;
	
	public IndexPhoto get(String id) {
		return indexPhotoDao.get(id);
	}
	
	public Page<IndexPhoto> find(Page<IndexPhoto> page, IndexPhoto indexPhoto) {
		DetachedCriteria dc = indexPhotoDao.createDetachedCriteria();
		dc.add(Restrictions.eq(IndexPhoto.FIELD_DEL_FLAG, IndexPhoto.DEL_FLAG_NORMAL));
		dc.addOrder(Order.asc("createDate"));
		return indexPhotoDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(IndexPhoto indexPhoto) {
		indexPhotoDao.clear();
		indexPhotoDao.save(indexPhoto);
	}
	
	@Transactional(readOnly = false)
	public void save(List<IndexPhoto> indexPhotos) {
		indexPhotoDao.clear();
		indexPhotoDao.save(indexPhotos);
	}
	
	@Transactional(readOnly = false)
	public void delete(String id) {
		indexPhotoDao.deleteById(id);
	}
	
	/**
	 * 获取商户对应的mall模块的首页设置信息
	 * @param merchantId
	 * @return
	 */
	public List<IndexPhoto> findListByMerhantId(String merchantId){
		DetachedCriteria dc = indexPhotoDao.createDetachedCriteria();
		dc.add(Restrictions.eq(IndexPhoto.FIELD_DEL_FLAG, IndexPhoto.DEL_FLAG_NORMAL));
		dc.add(Restrictions.eq("merchant.id", merchantId));
		dc.addOrder(Order.asc("createDate"));
		return indexPhotoDao.find(dc);
	}
	
	/**
	 * 获取mall模块的首页设置信息
	 * @return
	 */
	public List<IndexPhoto> findList(){
		DetachedCriteria dc = indexPhotoDao.createDetachedCriteria();
		dc.add(Restrictions.eq(IndexPhoto.FIELD_DEL_FLAG, IndexPhoto.DEL_FLAG_NORMAL));
		dc.addOrder(Order.asc("createDate"));
		return indexPhotoDao.find(dc);
	}

}
